package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dataTransmit.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .eq(shoppingCartDTO.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishId()!=null,ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getDishFlavor()!=null,ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor());
        ShoppingCart shoppingCart = getOne(queryWrapper);
        if(shoppingCart!=null){
            //购物车里的物品已存在
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<ShoppingCart>()
                    .eq(ShoppingCart::getId, shoppingCart.getId())
                    .set(ShoppingCart::getNumber, shoppingCart.getNumber() + 1);
            update(updateWrapper);
        }else {
            ShoppingCart cart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
            if(shoppingCartDTO.getSetmealId()!=null){
                //套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                cart.setImage(setmeal.getImage());
                cart.setName(setmeal.getName());
                cart.setAmount(setmeal.getPrice());
            }else {
                //菜品
                Dish dish = dishMapper.getDishById(shoppingCartDTO.getDishId());
                cart.setImage(dish.getImage());
                cart.setName(dish.getName());
                cart.setAmount(dish.getPrice());
            }
            cart.setUserId(BaseContext.getCurrentId());
            cart.setNumber(1);
            save(cart);
        }
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .eq(shoppingCartDTO.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishId()!=null,ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getDishFlavor()!=null,ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor());
        ShoppingCart shoppingCart = getOne(queryWrapper);
        if(shoppingCart.getNumber()==1){
            removeById(shoppingCart.getId());
        }else {
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<ShoppingCart>()
                    .eq(ShoppingCart::getId, shoppingCart.getId())
                    .set(ShoppingCart::getNumber, shoppingCart.getNumber() - 1);
            update(updateWrapper);
        }
    }

    @Override
    public List<ShoppingCart> getList() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return list(queryWrapper);
    }

    @Override
    public void clear() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        remove(queryWrapper);
    }
}
